package com.example.kafka.controller;

import com.example.kafka.common.Response;
import com.example.kafka.factory.MgtCollectorFactory;
import com.example.kafka.pojo.MetricDataFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/mockRequest")
public class MetricController {

    @Autowired
    MgtCollectorFactory factory;

    @RequestMapping(value = "getMetricData", method = RequestMethod.POST)
    public Response<Void> getMetricData(@RequestBody MetricDataFilter param) {
        if (CollectionUtils.isEmpty(param.getUids())) {
            return new Response<>();
        }
        Map<String, List<String>> uidMap = param.getUids().stream().distinct().collect(Collectors.groupingBy(id -> factory.getSourceByUid(id)));
        for (Map.Entry<String, List<String>> entry : uidMap.entrySet()) {
            param.setUids(entry.getValue());
        }
        return Response.success(null);
    }

}
